package com.lin.csdn.service.impl;

import com.lin.csdn.dao.impl.BlogDao;
import com.lin.csdn.dao.impl.BrowseDao;
import com.lin.csdn.dao.impl.UserDao;
import com.lin.csdn.model.po.Messege;
import com.lin.csdn.service.intf.IBrowseService;
import com.lin.csdn.utils.InputSteam;
import com.lin.csdn.utils.OutputSteam;

import java.io.File;

public class BrowseService implements IBrowseService {
    BrowseDao browseDao=new BrowseDao();
    OutputSteam outputSteam=new OutputSteam();
    UserDao userDao=new UserDao();
    BlogDao blogDao=new BlogDao();
    @Override
    public String getContext(String blogId) {
         String url=browseDao.getArticle(Integer.parseInt(blogId.replace(" ","")));
         //去除因为空格引起的类型转换失败
         File file=new File(url);
        InputSteam inputSteam=new InputSteam();
        String result=inputSteam.getContext(file);
        return result;
    }

    @Override
    public String getPicurl(String blogId){
        String url=browseDao.getPic(Integer.parseInt(blogId.replace(" ","")));
        //去除因为空格引起的类型转换失败
        return url;
    }

    //更新标题，简介以及领域
    public Messege updateTdd(String blogId, String title, String description,String domain){
        int result=browseDao.updateTdd(Integer.parseInt(blogId.replace(" ","")),title,description,domain);
        return new Messege("修改成功",result,200);
    }

    //更新文章文本
    public Messege updateArticle(String blogId,String article){
        String url=browseDao.getArticle(Integer.parseInt(blogId.replace(" ","")));
        boolean result=outputSteam.outArticle(article,url);
        return  new Messege("修改成功",result,200);
    }

    @Override
    public void collect(String blogId,String userName){
        int userId=userDao.findId(userName);
        browseDao.collect(Integer.parseInt(blogId.replace(" ","")),userId);
    }

    @Override
    public void target(String blogId,String userName){
        int userId=userDao.findId(userName);
        String targetId=blogDao.getUserId(Integer.parseInt(blogId.replace(" ","")));
        browseDao.target(Integer.parseInt(targetId),userId);
    }

    //判断是否已经关注
    public boolean idFollow(String blogId,String userName){
        int userId=userDao.findId(userName);
        String targetId=blogDao.getUserId(Integer.parseInt(blogId.replace(" ","")));
        String result=browseDao.isFollow(Integer.parseInt(targetId),userId);
        return result.equals("404");
    }

    //判断是否已经收藏
    public boolean idCollect(String blogId,String userName){
        int userId=userDao.findId(userName);
        String result=browseDao.isCollect(Integer.parseInt(blogId.replace(" ","")),userId);
        return result.equals("404");
    }

}
